using System;
using System.Text;
using Neo.MetaModel;

namespace Webbness.VsTool
{
    [Serializable]
    public class HtmlHelpers
    {
        //private const int _size = 50;

        public bool DisplayAsHidden(Entity entity, EntityAttribute attribute)
        {
            if (attribute.IsIdentityColumn)
            {
                return true;
            }
            if (attribute.IsPkColumn)
            {
                if (entity.IdMethod == IdMethod.Guid)
                {
                    return true;
                }
                if (entity.IdMethod == IdMethod.IdBroker)
                {
                    return false;
                }
                if (entity.IdMethod == IdMethod.Native)
                {
                    return false;
                }
                if (entity.IdMethod == IdMethod.None)
                {
                    return false;
                }
            }
            return false;
        }

        public string GenerateControlReadOnly(Entity entity, EntityAttribute attribute)
        {
            try
            {
                EntityRelationship rel = entity.RelationshipForAttribute(attribute);
                if (rel == null)
                {
                    if (!attribute.DotNetType.Equals("System.Boolean"))
                    {
                        if (attribute.ColumnType.ToUpper().Equals("VARCHAR") && attribute.Size.Equals("max", StringComparison.InvariantCultureIgnoreCase))
                        {
                            return "<% Output(" + entity.ClassName.ToLower() + "." + attribute.DotNetName + "); %>";
                        }
                        if (attribute.ColumnType.ToUpper().Equals("TEXT") || (attribute.DotNetType.Equals("System.String") && (Convert.ToInt32(attribute.Size) > 250)))
                        {
                            return "<% Output(" + entity.ClassName.ToLower() + "." + attribute.DotNetName + "); %>";
                        }
                    }
                    return "<% Output(" + entity.ClassName.ToLower() + "." + attribute.DotNetName + "); %>";
                }
                if (rel.Direction == RelDirection.Child)
                {
                    //StringBuilder sb = new StringBuilder();
                    string foreignKeyDotNetName = string.Empty;
                    foreach (EntityAttribute fAttr in rel.ForeignEntity.Attributes)
                    {
                        if (fAttr.ColumnName.Equals(rel.ForeignKey))
                        {
                            foreignKeyDotNetName = fAttr.DotNetName;
                        }
                    }
                    if (foreignKeyDotNetName.Length == 0)
                    {
                        return "Unable to define relationship";
                    }
                    return "<% Output(" + entity.ClassName.ToLower() + "." + rel.DotNetName + "); %>";
                    //return sb.ToString();
                }
                return "<% Output(" + entity.ClassName.ToLower() + "." + attribute.DotNetName + "); %>";
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //Console.WriteLine(new StringBuilder("*** For table \"").Append(entity.TableName).Append("\" column name ").Append(attribute.ColumnName).Append(" :") + e.InnerException);
                return "<% Output(" + entity.ClassName.ToLower() + "." + attribute.DotNetName + "); %>";
            }
        }

        public string GenerateControlWithValue(Entity entity, EntityAttribute attribute)
        {
            string tbSize = string.Empty;
            try
            {
                StringBuilder sb = new StringBuilder();
                EntityRelationship rel = entity.RelationshipForAttribute(attribute);
                string bokOrNot = string.Empty;
                if (!attribute.IsRequired)
                {
                    bokOrNot = "|bok";
                }
                if (rel == null)
                {
                    if (attribute.IsReadOnly)
                    {
                        return "<%= " + "CustomHtmlHelper.InputReadOnly( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ")" + " %>";
                    }
                    if (attribute.DotNetType.Equals("System.Boolean"))
                    {
                        return "<%= " + "CustomHtmlHelper.InputTrueFalseRadio(\"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ")" + " %>";
                    }
                    if (attribute.ColumnType.ToUpper().Equals("VARCHAR") && attribute.Size.Equals("max", StringComparison.InvariantCultureIgnoreCase))
                    {
                        return "<%= " + "CustomHtmlHelper.TextAreaWithEditor( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
                    }
                    if (attribute.ColumnType.ToUpper().Equals("TEXT"))
                    {
                        return "<%= " + "CustomHtmlHelper.TextAreaWithEditor( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
                    }
                    if (attribute.DotNetType.Equals("System.String") && (Convert.ToInt32(attribute.Size) > 250))
                    {
                        return "<%= " + "CustomHtmlHelper.TextArea( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
                    }
                    if (attribute.DotNetType.Equals("System.DateTime"))
                    {
                        return "<%= " + "CustomHtmlHelper.CustomDateTimePicker( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
                    }
                    if (attribute.DotNetType.Equals("System.Guid"))
                    {
                        return "<%= " + "CustomHtmlHelper.InputText( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
                    }
                    if (isNumber(attribute.DotNetType))
                    {
                        return "<%= " + "CustomHtmlHelper.InputText( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", " + 50 + ", 4, this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
                    }
                    tbSize = attribute.Size;
                    if (string.IsNullOrEmpty(attribute.Size))
                    {
                        tbSize = "4";
                    }
                    return "<%= " + "CustomHtmlHelper.InputText( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", " + 50 + ", " + tbSize + ", this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
                }
                if (rel.Direction == RelDirection.Child)
                {
                    string foreignKeyDotNetName = string.Empty;
                    foreach (EntityAttribute fAttr in rel.ForeignEntity.Attributes)
                    {
                        if (fAttr.ColumnName.Equals(rel.ForeignKey))
                        {
                            foreignKeyDotNetName = fAttr.DotNetName;
                        }
                    }
                    if (foreignKeyDotNetName.Length == 0)
                    {
                        throw new Exception();
                    }
                    if (attribute.IsReadOnly)
                    {
                        return "<%= " + "CustomHtmlHelper.InputReadOnly( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + rel.DotNetName + ")" + " %>";
                    }
                    sb.Append("<%= " + "this.Helpers.Html.Select( \"" + entity.ClassName + "." + attribute.DotNetName + "\", this.Helpers.Dict.CreateDict(\"validators=select" + bokOrNot + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>");
                    sb.Append(Environment.NewLine);
                    sb.Append("<%= " + "CustomHtmlHelper.CreateOptions( " + rel.ForeignEntity.ClassName.ToLower() + "s, \"" + rel.ForeignEntity.DefaultProperty + "\", \"" + foreignKeyDotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", OptionsType.MakeSelection )" + " %>");
                    sb.Append(Environment.NewLine);
                    sb.Append(("<%= " + "this.Helpers.Html.EndSelect()") + " %>");
                    return sb.ToString();
                }
                tbSize = attribute.Size;
                if (string.IsNullOrEmpty(attribute.Size))
                {
                    tbSize = "4";
                }
                return "<%= " + "CustomHtmlHelper.InputText( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", " + 50 + ", " + tbSize + ", this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return "<%= " + "CustomHtmlHelper.InputText( \"" + entity.ClassName + "." + attribute.DotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", " + 50 + ", " + tbSize + ", this.Helpers.Dict.CreateDict(\"validators=" + attribute.InputValidator + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>";
            }
        }

        public string GenerateSelect(Entity entity, EntityAttribute attribute, int listType)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                EntityRelationship rel = entity.RelationshipForAttribute(attribute);
                if (rel == null)
                {
                    return "<!-- for Entity " + entity + " the Attribute " + attribute + " is not a relationship. -->";
                }
                if (rel.Direction == RelDirection.Child)
                {
                    string foreignKeyDotNetName = string.Empty;
                    foreach (EntityAttribute fAttr in rel.ForeignEntity.Attributes)
                    {
                        if (fAttr.ColumnName.Equals(rel.ForeignKey))
                        {
                            foreignKeyDotNetName = fAttr.DotNetName;
                        }
                    }
                    if (foreignKeyDotNetName.Length == 0)
                    {
                        throw new Exception();
                    }
                    string bokOrNot = string.Empty;
                    if (!attribute.IsRequired)
                    {
                        bokOrNot = "|bok";
                    }
                    sb.Append("<%= " + "this.Helpers.Html.Select( \"" + entity.ClassName + "." + attribute.DotNetName + "\", this.Helpers.Dict.CreateDict(\"validators=select" + bokOrNot + "\", \"emsg=\" + " + entity.ClassName + "Resource.EMsg_" + attribute.DotNetName + " ) )" + " %>");
                    sb.Append(Environment.NewLine);
                    string optionType = "OptionsType.MakeSelection";
                    if (listType == 0)
                    {
                        optionType = "OptionsType.None";
                    }
                    else if (listType == 1)
                    {
                        optionType = "OptionsType.MakeSelection";
                    }
                    else if (listType == 2)
                    {
                        optionType = "OptionsType.SelectAll";
                    }
                    sb.Append("<%= " + "CustomHtmlHelper.CreateOptions( " + rel.ForeignEntity.ClassName.ToLower() + "s, \"" + rel.ForeignEntity.DefaultProperty + "\", \"" + foreignKeyDotNetName + "\", " + entity.ClassName.ToLower() + "." + attribute.DotNetName + ", " + optionType + " )" + " %>");
                    sb.Append(Environment.NewLine);
                    sb.Append(("<%= " + "this.Helpers.Html.EndSelect()") + " %>");
                    return sb.ToString();
                }
                return "<!-- for Entity " + entity + " the Attribute " + attribute + " is not a child relationship. -->";
            }
            catch (Exception e)
            {
                return "<!-- For table \"" + entity.TableName + "\" column name " + attribute.ColumnName + " : " + e.InnerException + " -->";
            }
        }

        public string GetSortName(Entity entity, EntityAttribute attribute)
        {
            try
            {
                EntityRelationship rel = entity.RelationshipForAttribute(attribute);
                if ((rel != null) && (rel.Direction == RelDirection.Child))
                {
                    string foreignKeyDotNetName = string.Empty;
                    foreach (EntityAttribute fAttr in rel.ForeignEntity.Attributes)
                    {
                        if (fAttr.ColumnName.Equals(rel.ForeignKey))
                        {
                            foreignKeyDotNetName = fAttr.DotNetName;
                        }
                    }
                    if (foreignKeyDotNetName.Length == 0)
                    {
                        return "Unable to define relationship";
                    }
                    if (string.IsNullOrEmpty(rel.ForeignEntity.DefaultProperty))
                    {
                        return rel.DotNetName;
                    }
                    return rel.DotNetName + "." + rel.ForeignEntity.DefaultProperty;
                }
                return attribute.DotNetName;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                //Console.WriteLine(new StringBuilder("*** For table \"").Append(entity.TableName).Append("\" column name ").Append(attribute.ColumnName).Append(" :") + e.InnerException);
                return attribute.DotNetName;
            }
        }

        private bool isNumber(string inVal)
        {
            return ((inVal.Equals("System.Int16") || inVal.Equals("System.Int32")) || ((inVal.Equals("System.Int64") || inVal.Equals("System.Single")) || inVal.Equals("System.Double")));
        }
    }
}